package cn.tedu.superscholarstore.filter;




import cn.tedu.superscholarstore.pojo.vo.UserVO;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "MyFilter",urlPatterns = {"/favorites.html","/favorites/*","/admin.html"})
public class MyFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest rt = (HttpServletRequest) request;
        HttpServletResponse rs =  (HttpServletResponse) response;
        HttpSession session = rt.getSession();
        UserVO vo = (UserVO) session.getAttribute("user");
        if(vo!=null){
            chain.doFilter(request, response);
        }else {
            rs.sendRedirect("/login.html");
        }

    }
}
